package org.gruposp2p.proyectokarma.gwt.client.panel;

import org.gruposp2p.proyectokarma.gwt.client.domain.Document;
import org.gruposp2p.proyectokarma.gwt.client.event.EventBusManager;
import org.gruposp2p.proyectokarma.gwt.client.event.TagEvent;
import org.gruposp2p.proyectokarma.gwt.client.event.TagEvent.Action;
import org.gruposp2p.proyectokarma.gwt.client.ui.TopPanel;
import org.gruposp2p.proyectokarma.gwt.client.util.DateUtils;
import org.gruposp2p.proyectokarma.gwt.client.util.ServerPaths;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.allen_sauer.gwt.log.client.Log;

public class DocumentPanel extends Composite {

	private static DocumentPanelUiBinder uiBinder = GWT.create(DocumentPanelUiBinder.class);
	interface DocumentPanelUiBinder extends UiBinder<Widget, DocumentPanel> { }
	
    interface Style extends CssResource {
        String author();
        String authorPanel();
        String dateCreated();
        String tagLabel();
        String tagsPanel();
    }

	@UiField VerticalPanel mainPanel;
    @UiField VerticalPanel votesPanel;
    @UiField Style style;
    @UiField VerticalPanel commentsPanel;
    @UiField VerticalPanel documentTitlePanel;
    @UiField HorizontalPanel tagsPanel;
    @UiField HorizontalPanel authorPanel;
    @UiField HTML contentPanel;
    @UiField HTML title;
    @UiField HTML dateCreated;
    @UiField HTML author;
    @UiField Image qrImage;

	
	public DocumentPanel(Document document) {
		initWidget(uiBinder.createAndBindUi(this));
    	TagClickHandler tagClickHandler = new TagClickHandler();
    	TitleClickHandler titleClickHandler = new TitleClickHandler();
    	title.setText(document.getName());
    	contentPanel.setHTML(document.getContent());
    	qrImage.setUrl(ServerPaths.getQRCodeUrl(
				90, 90, ServerPaths.getDocumentPath(document.getId())));
    	qrImage.addClickHandler(titleClickHandler);
		dateCreated.setText(DateUtils.getSimpleStringFromDate(
				document.getDateCreated()));
		author.setText(document.getPeerName());
		authorPanel.add(dateCreated);
		authorPanel.add(author);
		 if (document.getTags() != null) {
			for (String tag : document.getTags()) {
				Anchor tagAnchor = new Anchor(tag);
				tagAnchor.addClickHandler(tagClickHandler);
				tagAnchor.setStylePrimaryName(style.tagLabel());
				tagsPanel.add(tagAnchor);
			}
		} 
		
	}
	
    private class TagClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			String selectedTag = ((Anchor)event.getSource()).getText();
			TagEvent tagEvent = new TagEvent(Action.GET_DOCUMENTS, selectedTag);
			EventBusManager.getInstance().fireEvent(tagEvent);
        	History.newItem(TopPanel.HISTORY_TAG);
        	History.fireCurrentHistoryState(); 
		}	
	}
    
    private class TitleClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			String selectedTag = ((Anchor)event.getSource()).getText();
			TagEvent tagEvent = new TagEvent(Action.GET_DOCUMENTS, selectedTag);
			EventBusManager.getInstance().fireEvent(tagEvent);
        	History.newItem(TopPanel.HISTORY_TAG);
        	History.fireCurrentHistoryState(); 
		}	
	}

    public String getTruncatedContent(String content) {
        if (content != null && content.length() > 500) {
        	return content.substring(0, 500) + "...";
        }
    	return content;
    }
}
